﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}

<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Quick Start - Layui</title>
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <div class="layui-bg-gray" style="padding: 16px;" id="Inquire">
        <div class="layui-panel" style="width:100%;">
            <div class="layui-card">
                <div class="layui-card-header">
                    查询条件
                </div>
                @* 主体 *@
                <div class="layui-card-body">
                    <form class="layui-form layui-row layui-col-space16" lay-filter="demo-val-filter" action="">
                        <div class="layui-col-md5">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">工艺编码</label>
                                <div class="layui-input-block">
                                    <input type="text" id="processRouteNo" placeholder="请输入" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md5">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">工艺名称</label>
                                <div class="layui-input-block">
                                    <input type="text" id="processRouteName" placeholder="请输入" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md5">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">物料</label>
                                <div class="layui-input-block">
                                    <select id="mySelect" lay-filter="demoselect">
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md5">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-block">
                                    <select id="processStatus">
                                        <option value="0">请选择</option>
                                        <option value="1">有效</option>
                                        <option value="2">无效</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md5"></div>

                        <div class="layui-col-md5">
                            <div class="layui-form-item" style="float:right;">
                                <div class="layui-input-block">
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    <button type="submit" id="LookInquire" class="layui-btn layui-btn-normal" lay-submit>查询</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-bg-gray" style="padding: 16px;">
        <div class="layui-panel">
            <div class="layui-card">
                <div class="layui-card-header">
                    <span style="font:bolder">工艺路线列表</span>
                    @* 弹出框 *@
                    <button type="button" style="float:right" class="layui-btn layui-btn-primary layui-border layui-icon layui-icon-search" id="Reveal">展开查询</button>
                    <button type="button" style="float:right" class="layui-btn layui-icon layui-icon-addition" onclick="location.href='/Process/CreateProcessRoute'">新增工艺</button>
                </div>
                <div class="layui-card-body">
                    @* 表格 *@
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th>序号</th>
                                <th>工艺编码</th>
                                <th>工艺名称</th>
                                <th>物料</th>
                                <th>工艺版本</th>
                                <th>创建时间</th>
                                <th>创建人</th>
                                <th>状态</th>
                                <th>审批状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                    <div id="demo-laypage-all"></div>
                </div>
            </div>
        </div>
    </div>


    <script>
        // 使用layui的layer模块
        layui.use(['form', 'table', 'layer'], function () {
            var $ = layui.$;
            var form = layui.form;
            var table = layui.table;
            var laypage = layui.laypage;

            //获取分页数据
            var pageIndex = 1;
            var pageSize = 3;

            $(function () {
                $('#Inquire').toggle();
                ObtainMaterial();//物料
                SearchProcessRoute(pageIndex, pageSize);//工艺路线列表
            })


            // 绑定按钮点击事件
            $('#Reveal').on('click', function () {
                $('#Inquire').toggle();
            });

            // 绑定查询按钮点击事件
            $('#LookInquire').on('click', function () {
                SearchProcessRoute(pageIndex, pageSize);
                return false; // 阻止默认 form 跳转
            });

            //获取物料
            function ObtainMaterial() {
                $.ajax({
                    url: '@ViewBag.RedDragonWrite' + "/api/Process/SearchLogistics",
                    type: 'GET',
                    dataType: 'json',
                    success: res => {
                        var options = '<option value="0">请选择</option>';
                        $.each(res.data, function (index, item) {
                            options += '<option value="' + item.materialId + '">' + item.materialName + '</option>';
                        });
                        $('#mySelect').html(options);
                        form.render('select'); // 更新下拉框渲染
                    }
                });
            }

            //获取工艺路线列表
            function SearchProcessRoute(index, size) {
                //获取数据
                var data = {
                    pageIndex: index,
                    pageSize: size,
                    processRouteNo: $('#processRouteNo').val(),
                    processRouteName: $('#processRouteName').val(),
                    materialId: $('#mySelect').val() || 0,
                    processStatus: $('#processStatus').val()
                }
                console.log(data);
                var totalCount = 0;//总条数
                var totalPage = 0;//总页数
                //获取
                $.ajax({
                    url: '@ViewBag.RedDragonWrite' + "/api/Process/SearchProcessRoute",
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: "application/json",
                    success: res => {
                        console.log(res)
                        $("tbody").empty();
                        if (res.data != null) {
                            totalCount = res.data.totalCount;
                            totalPage = res.data.pageCount;
                            $.each(res.data.list, function (index, item) {
                                item.createdTime = moment(item.createdTime).format("YYYY-MM-DD HH:mm:ss")
                                $("tbody").append(`<tr>` +
                                    `<td>${item.processRouteId}</td>` +
                                    `<td>${item.processRouteNo}</td>` +
                                    `<td>${item.processRouteName}</td>` +
                                    `<td>${item.materialName}</td>` +
                                    `<td>${item.processVersion}</td>` +
                                    `<td>${item.createdTime}</td>` +
                                    `<td>${item.createdOwner}</td>` +
                                    `<td>${item.processStatus == 1 ? "有效" : "无效"}</td>` +
                                    `<td>${item.auditStatus == 1 ? "未提交" : "已提交"}</td>` +
                                    `<td><button type="button" class="layui-btn layui-btn-primary layui-btn-xs layui-border layui-icon layui-icon-search" onclick="DetailProcessRoute('${item.processRouteNo}')">详情</button></td>` +
                                    `</tr>`)
                            });
                        }
                        laypage.render({
                            elem: 'demo-laypage-all',
                            curr: data.pageIndex,//当前页
                            limit: data.pageSize,//每页显示条数
                            limits: [3, 6, 9, 12],//每页条数的选择项
                            count: totalCount, // 数据总数，从后端得到
                            first: "首页",
                            prev: "上一页",
                            next: "下一页",
                            last: "尾页",
                            layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'limit', 'skip'], // 自定义布局
                            jump: function (obj, first) {
                                console.log("分页", obj);
                                // 首次不执行
                                if (!first) {
                                    pageIndex = obj.curr;
                                    pageSize = obj.limit;
                                    SearchProcessRoute(pageIndex, pageSize);
                                }
                            }
                        });
                    }
                });
            }
        });

        function DetailProcessRoute(processRouteNo) {
            //跳转页面
            location.href = "/Process/ProcessLineOperationDetails?processRouteNo=" + processRouteNo;
        }
    </script>
</body>
</html>
